#include "uiCommon.hpp"
#include "uiMainLayer.h"
#include "uiNodeLayer.h"
#include "AppMacros.h"

USING_NS_CC;

uiMainLayer::~uiMainLayer(){
    smuDelete( _clickChecker );
}

Scene* uiMainLayer::scene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    uiMainLayer *layer = uiMainLayer::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool uiMainLayer::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !LayerColor::initWithColor( Color4B( 56, 58, 76, 255 ) ) )
    {
        return false;
    }
    _clickChecker = new uiDoubleClickChecker();
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    //auto origin = Director::getInstance()->getVisibleOrigin();
    
    // create node layer
    _nodeLayer = uiNodeLayer::create();
    
    _scrollView = ScrollView::create( Size( visibleSize.width, visibleSize.height ) );
    _scrollView->setContainer( _nodeLayer );
    _scrollView->setPosition( Point::ZERO );
    _scrollView->setDirection( extension::ScrollView::Direction::BOTH );
    _scrollView->setContentOffset( Point::ZERO );
    _scrollView->setContentSize( Size( visibleSize.width, visibleSize.height ) );
    _scrollView->setDelegate( this );
    this->addChild( _scrollView );
    
    auto listener = EventListenerTouchOneByOne::create();    
    listener->onTouchBegan = CC_CALLBACK_2(uiMainLayer::onTouchBegan, this);
    listener->onTouchEnded = CC_CALLBACK_2(uiMainLayer::onTouchEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, _nodeLayer);
    
    auto keylistener = EventListenerKeyboard::create();
    keylistener->onKeyReleased = CC_CALLBACK_2(uiMainLayer::onKeyReleased, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority( keylistener, _nodeLayer );
    
    Size editSize( visibleSize.width / 2, g_font_size * 5 );
    _editBox = cocos2d::ui::EditBox::create( editSize, ui::Scale9Sprite::create( "input.png" ) );
    _editBox->setInputMode( ui::EditBox::InputMode::SINGLE_LINE );
    _editBox->setDelegate( this );
    this->addChild( _editBox );
    _editBox->setPosition( Vec2( visibleSize.width / 2, visibleSize.height / 2 ) );
    _editBox->setVisible( false );
    
    return true;
}

void uiMainLayer::menuCloseCallback(Ref* sender)
{
    Director::getInstance()->end();
}


void uiMainLayer::editBoxReturn( ui::EditBox* editBox )
{
    if( !editBox ){
        return;
    }
    std::string str = editBox->getText();
    
    if( str.length() == 0 ){
        return;
    }
    _editBox->setText( "" );
    _editBox->setVisible( false );
    uiNodeManager::getInstance().processString( str, false );
    uiNodeManager::getInstance().endAddNodes();
}


bool uiMainLayer::onTouchBegan(Touch *touch, Event *event)
{
    _editBox->setText( "" );
    _editBox->setVisible( false );
    return true;
}

void uiMainLayer::onTouchEnded(Touch *touch, Event *event)
{
    Layer::onTouchEnded( touch, event );
    _clickChecker->setTime();
    if( _clickChecker->isDoubleClick() ){
        _editBox->setText("");
        _editBox->setVisible( true );
        _editBox->touchDownAction( nullptr, ui::EditBox::TouchEventType::ENDED );
    }
}

void uiMainLayer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
    if( keyCode == EventKeyboard::KeyCode::KEY_Q ){
        Director::getInstance()->end();
    }
}
